package com.beamfield.controller.api;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.modelVo.WVo;
import com.beamfield.modelVo.YVo;
import com.beamfield.service.api.ApiLabService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/api/lab")
public class ApiLabController {

    private final ApiLabService apiLabService;

    @Autowired
    public ApiLabController(ApiLabService apiLabService) {
        this.apiLabService = apiLabService;
    }

    /**
     * 万用机数据接口
     *
     * @return
     */
    @RequestMapping("/syncW")
    @ResponseBody
    public ApiResult saveWData(@RequestParam(value = "jsonData", required = false) String jsonData) {
        System.out.println(jsonData);
        if (StringUtils.isEmpty(jsonData)) {
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            WVo wVo = JSON.parseObject(jsonData, WVo.class);
            apiLabService.saveWData(wVo.getwGroups(), wVo.getwDetails());
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        return new ApiResult();
    }

    /**
     * 压力机数据接口
     *
     * @return
     */
    @RequestMapping("/syncY")
    @ResponseBody
    public ApiResult saveYData(@RequestParam(value = "jsonData", required = false) String jsonData) {
        System.out.println(jsonData);
        if (StringUtils.isEmpty(jsonData)) {
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            YVo yVo = JSON.parseObject(jsonData, YVo.class);
            apiLabService.saveYData(yVo.getyGroups(), yVo.getyDetails());
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        return new ApiResult();
    }

}
